#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include "account.h"
#include <cstring>

typedef struct{
    char name[20];
    Account*account;
    double amt;

}operarg;


void*withdraw_fn(void*arg){
    operarg *oa=(operarg*)arg;
    double amt=withdraw(oa->account,oa->amt);//取出amt这些金额
    std::cout<<"name"<<oa->name<<"  thread id "<<pthread_self()<<" amt::"<<amt<<std::endl;
    return (void*)0;
}




//定义存款操作的线程运行函数
void*deposit_fn(void*arg){

    operarg *oa=(operarg*)arg;
    double amt=deposit(oa->account,oa->amt);
    std::cout<<"name"<<oa->name<<"  thread id "<<pthread_self()<<" amt::"<<amt<<std::endl;
    return (void*)0;

    
}



void*check_fn(void*arg){


    return (void*)0;
}


int main(){
    int err;
    pthread_t boy,girl;
    Account*a=create_account(1001,10000);

    //两个用户操作同一个账户
    operarg o1,o2;
    o1.account=a;
    strcpy(o1.name,"boy");
    o1.amt=10000;

    o2.account=a;
    o2.amt=10000;
    strcpy(o2.name,"girl");

    pthread_t t1,t2;
    if((err=pthread_create(&t1,NULL,withdraw_fn,(void*)&o1))<0){
        perror("pthread_create");
        exit(1);
    }    


    if((err=pthread_create(&t2,NULL,withdraw_fn,(void*)&o2))<0){
        perror("pthread_create");
        exit(1);
    }






    pthread_join(t1,NULL);
    pthread_join(t2,NULL);

    std::cout<<"account balance"<<get_balance(a)<<std::endl;
    destroy_account(a);

    return 0;
}

